package com.ruoyi.web.controller.tongban;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tongban.domain.FaceSign;
import com.ruoyi.tongban.service.IFaceSignService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤记录Controller
 * 
 * @author caiyanshan
 * @date 2021-04-08
 */
@RestController
@RequestMapping("/tongban/sign")
public class FaceSignController extends BaseController
{
    @Autowired
    private IFaceSignService faceSignService;
    
    /**
     * 查询考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('tongban:sign:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaceSign faceSign)
    {
        if(faceSign.getUserType() == null){
            faceSign.setUserType(0);
        }
        if(faceSign.getSearchTime() == null){
            faceSign.setSearchTime(DateUtils.getDate("YYYY-MM"));
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(!SecurityUtils.isAdmin(user.getUserId())){
            faceSign.setDeptId(user.getDeptId());
        }
        
        startPage();
        List<FaceSign> list = faceSignService.selectFaceSignList(faceSign);
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < list.size(); i++){
            sb.append(list.get(i).getUserId());
            if(i != list.size()-1){
                sb.append(",");
            }
        }
        faceSign.setUserIds(sb.toString());
        List<FaceSign> tempList = faceSignService.selectFaceSignList(faceSign);

        List<Map<String,String>> signList = null;
        Map<String, String> signMap = null;

        int month = DateUtils.getMonth(DateUtils.parseDateTemp(faceSign.getSearchTime()));

        int maxDays = DateUtils.getDaysByYearMonth(month);
        for(FaceSign faceSignTemp : list){
            signList = new ArrayList<>();
            for(int i = 1; i<= maxDays; i++){
                signMap = new HashMap<>();
                signMap.put("day" + i, month + "/" + i);
                for(FaceSign faceSignItem : tempList){
                    int day = DateUtils.getDay(faceSignItem.getCreateTime());
                    if(day == i){
                        if(faceSignTemp.getUserId().longValue() == faceSignItem.getUserId().longValue()){
                            signMap.put("value", DateUtils.getSignTime(faceSignItem.getSignIn())
                                    + "|"+ DateUtils.getSignTime(faceSignItem.getSignOut()));
                        }
                    }
                }
                signList.add(signMap);
            }
            faceSignTemp.setSignListData(signList);
        }

        return getDataTable(list);
    }

    /**
     * 导出考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('tongban:sign:export')")
    @Log(title = "考勤记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FaceSign faceSign)
    {
        List<FaceSign> list = faceSignService.selectFaceSignList(faceSign);
        ExcelUtil<FaceSign> util = new ExcelUtil<FaceSign>(FaceSign.class);
        return util.exportExcel(list, "sign");
    }

    /**
     * 获取考勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('tongban:sign:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(faceSignService.selectFaceSignById(recordId));
    }

    /**
     * 新增考勤记录
     */
    @PreAuthorize("@ss.hasPermi('tongban:sign:add')")
    @Log(title = "考勤记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaceSign faceSign)
    {
        return toAjax(faceSignService.insertFaceSign(faceSign));
    }

    /**
     * 修改考勤记录
     */
    @PreAuthorize("@ss.hasPermi('tongban:sign:edit')")
    @Log(title = "考勤记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaceSign faceSign)
    {
        return toAjax(faceSignService.updateFaceSign(faceSign));
    }

    /**
     * 删除考勤记录
     */
    @PreAuthorize("@ss.hasPermi('tongban:sign:remove')")
    @Log(title = "考勤记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(faceSignService.deleteFaceSignByIds(recordIds));
    }
}
